var app = getApp();
Page({
  data: {
    goodsList:[],
    totalPrice:0,
    status:false,
    showView: true,
    allSelect: true,
    integral:'',
    resourceUrl: app.globalData.apiUrl + '/public/uploads/goods/',
  },
  onLoad: function (options) {
    var that = this;
    //初始化总价
    that.totalPrice()
    
    
    
  },
  change: function () {
    var that = this;
    
    that.setData({
      showView: (!that.data.showView)
    })
  },
  onShow: function (options){
    var that = this;
   
    //购物车数据
    var shopCarInfo = wx.getStorageSync('shopCarInfo')
    try{
      if(shopCarInfo.goodsList.length){

        that.setData({ goodsList: shopCarInfo, showView:false})
        
      }else{
        //购物车没有商品
        that.setData({showView:true})
      }
    }catch (err){
      console.log('报错...')
      console.log(err)
    }
   
  },
  /**
   * 选择商品
   */
  selectTap: function (tap) {
    var id = tap.currentTarget.dataset.id;//购物车商品id
    var shopCarInfo = wx.getStorageSync('shopCarInfo');

    for (var i in shopCarInfo.goodsList){
      if (id == shopCarInfo.goodsList[i].id){
        if (shopCarInfo.goodsList[i].active==1){
          shopCarInfo.goodsList[i].active = 0;
          }else{
          shopCarInfo.goodsList[i].active = 1;
          }
          
        }
    }
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    wx.switchTab({
      url: '/pages/cart/cart',
      success: function (res) {
        wx.showToast({
          title: '操作成功',
          icon: 'none',
        })
      }
    })
    this.setData({goodsList:shopCarInfo})
    this.totalPrice()

    
  },
  bindAllSelect:function(res){
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    for(var i in shopCarInfo.goodsList){
      shopCarInfo.goodsList[i].active=1;
    }
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    this.setData({ goodsList: shopCarInfo })
    this.totalPrice()
  },
  // 减
  jianBtnTap:function(res){
    console.log(res)
    var id = res.target.dataset.id;
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    for(var i in shopCarInfo.goodsList){
      if (shopCarInfo.goodsList[i].id == id){
          
        if (shopCarInfo.goodsList[i].num !=1){
              
          shopCarInfo.goodsList[i].num = shopCarInfo.goodsList[i].num-1
          }
        }
    }
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    this.setData({ goodsList: shopCarInfo })
    this.totalPrice_reduce(id)
  },
  //加
  jiaBtnTap:function(res){
    console.log(res)
    var id = res.target.dataset.id;
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    console.log(shopCarInfo)
    for (var i in shopCarInfo.goodsList) {
      if (shopCarInfo.goodsList[i].id == id) {
        var price = shopCarInfo.goodsList[i].price
        shopCarInfo.goodsList[i].num = shopCarInfo.goodsList[i].num + 1
        
      }
    }
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    this.setData({ goodsList: shopCarInfo })
    this.totalPrice_add(id)
  },
  //删除选中商品
  editTap:function(res){
    console.log(res)
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    var j=1;
    for(var i in shopCarInfo.goodsList){
      j = ++j;
      if(shopCarInfo.goodsList[i].active== true){
        shopCarInfo.goodsList.splice(i,j+1);
        continue;
      }
    }
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    this.setData({ goodsList: shopCarInfo })
    if(this.data.goodsList.goodsList<=0){
      this.setData({ showView:true})
   
    }
    
  },
  //总价格加
  totalPrice_add:function(id){
    //总价
    var that = this;
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    for (var i in shopCarInfo.goodsList) {
      if (shopCarInfo.goodsList[i].active == 1 && id == shopCarInfo.goodsList[i].id){
        shopCarInfo.totalPrice += shopCarInfo.goodsList[i].price
      }
      
    }
    that.totalPrice()
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    
  },
  // 总价减
  totalPrice_reduce: function (id) {
    //总价
    var that = this;
    var totalPrice = that.data.totalPrice
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    console.log(id)
    for (var i in shopCarInfo.goodsList) {
      if (shopCarInfo.goodsList[i].active == 1 && id == shopCarInfo.goodsList[i].id){
        shopCarInfo.totalPrice -= shopCarInfo.goodsList[i].price;
      }
      
    }
    that.totalPrice()
    wx.setStorageSync('shopCarInfo', shopCarInfo);
    // that.setData({ totalPrice: that.data.totalPrice-totalPrice })
  },
  /**
   * 总价
   */
  totalPrice:function(){
    var that = this;
    var totalPrice = 0;
    var shopCarInfo = wx.getStorageSync('shopCarInfo');
    for (var i in shopCarInfo.goodsList) {
      if (shopCarInfo.goodsList[i].active==1){
        totalPrice += shopCarInfo.goodsList[i].price * shopCarInfo.goodsList[i].num
      }
    }
    that.setData({ totalPrice: totalPrice })
  },
  toIndexPage:function(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //支付购物车中的订单
  toPayOrder:function(){
        console.log(111)
        wx.navigateTo({
          url: '/pages/to_pay/to_pay',
        })
        

      
  },


})